<%@page import="java.io.File"%>
<%@page import="java.io.FileInputStream"%>
<%@page import="java.io.IOException"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.SQLException"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Pregunta</title>
</head>
<body>

<%
	Object objTitle 		= request.getParameter("title");
	Object objDescription	= request.getParameter("description");
	Object objImage 		= request.getParameter("image");
	Object objLevel			= request.getParameter("level");
	Object objValue 		= request.getParameter("value");
	Object objAction 		= request.getParameter("action");
	
	out.print(objImage+"<br/>");
	
	if(objTitle != null && 
		objDescription != null &&
		objImage != null &&
		objLevel != null &&
		objValue != null &&
		objAction != null) {

		String strAction = objAction.toString();
		
		if(strAction.equals("Guardar")) {
			String strTitle       = objTitle.toString();
			String strDescription = objDescription.toString();
			String strImage       = objImage.toString();
			String strLevel       = objLevel.toString();
			String strValue       = objValue.toString();
			
			out.print(" strimage= "+strImage+"<br />");
			
			int intLevel;
			int intValue;

			try {
				intLevel = Integer.parseInt(strLevel);
			} catch(Exception e) {
				out.print("ERROR INT LEVEL");
				intLevel = -1;
			}

			try {
				intValue = Integer.parseInt(strValue);
			} catch(Exception e) {
				out.print("ERROR INT VALUE");
				intValue = -1;
			}

			try {
				//Cargar clase de controlador de base de datos
				Class.forName("com.mysql.jdbc.Driver");
				//Establecer conexion a la base de datos
			    Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1/qr2013", "root", "root");

			    String INSERT_QUERY = "INSERT INTO question(`title`, `description`, `level`, `image`, `imagePath`, `value`) VALUES (?, ?, ?, ?, ?, ?)";  
			    // in MYSQL --> INSERT INTO `qr2013`.`question` (`id`, `title`, `description`, `level`, `image`, `imagePath`, `value`, `status`) VALUES ('2', 'se', 'se', '4', ?, 'mapbok.png', '12', '0');
			    FileInputStream fis = null;
			    PreparedStatement ps = null;
			    
			    try { 	
				    
			      	conn.setAutoCommit(false);
					//strImage= "C:\\Users\\XPLOD\\Desktop\\bitcoin.jpg";
					//strImage= "Z:\\Imagenes\\mapbox.png";
			      	File file = new File(strImage); // strImage tiene la ruta de la imagen: C:\Users\herrea\Desktop\duke.gif
			      	fis = new FileInputStream(file);

			      	ps = conn.prepareStatement(INSERT_QUERY);

			      	ps.setString(      1, strTitle);
			      	ps.setString(      2, strDescription);
			      	ps.setInt(         3, intLevel);
			      	ps.setBinaryStream(4, fis, (int) file.length());
			      	ps.setString(      5, strImage.substring(strImage.lastIndexOf(File.separator) + 1));
			      	ps.setInt(         6, intValue);

			      	ps.executeUpdate();
			      	conn.commit();

%>
					<jsp:forward page="question.jsp" />
<%

			    } catch(Exception e){
			    	out.print(e.getMessage());
			    	
			    }finally {
			    	ps.close();
			    	fis.close();
			    }

			} catch(Exception e) {
				out.print("ERROR SQL");
				out.print(e.getMessage());
			}

		}

	} else {
		out.print("ERROR PARAMETERS NULL");

%>
		<jsp:forward page="question.jsp">
			<jsp:param name="error" value="Validation Error" />
		</jsp:forward>
<%
	}
%>

</body>
</html>